package com.nidu.demo.log.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.log.convertor.ApiAccessLogConvertor;
import com.nidu.demo.log.dto.ApiAccessLogCO;
import com.nidu.demo.log.gateway.ApiAccessLogGateway;
import com.nidu.demo.log.model.ApiAccessLog;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * API访问日志单个查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class ApiAccessLogSingleQryExe {

    private final ApiAccessLogGateway gateway;
    private final ApiAccessLogConvertor convertor;

    public SingleResponse<ApiAccessLogCO> execute(Long id) {
        // 1. 根据ID查询API访问日志
        ApiAccessLog apiAccessLog = gateway.getById(id);
        if (apiAccessLog == null) {
            throw new BizException(ErrorCodeConstants.API_ACCESS_LOG_NOT_FOUND.getCode(), ErrorCodeConstants.API_ACCESS_LOG_NOT_FOUND.getMessage());
        }

        // 2. 转换为DTO返回
        ApiAccessLogCO apiAccessLogCO = convertor.toClientObject(apiAccessLog);
        return SingleResponse.of(apiAccessLogCO);
    }

}
